//index.js
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    project_id: '',
    project_name: '',
    area: '',
    project: null,
    suit: '',
    update_date: '',
    products: [],
    category_ids: [],
    change_project_name: '',
    onshow: false,
    options: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    let _that = this;
    app.getToken().then(function () {
      _that.loadData(options);
    });
  },

  loadData: function (options) {
    //项目详情
    const that = this;

    wx.showLoading({
      title: '加载中',
    });
    wx.request({
      url: app.globalData.requestUrl + 'api/project/detail',
      method: 'POST',
      data: {
        user_token: app.globalData.userToken,
        project_id: options.id
      },
      success: function (res) {
        let area = 0,
          suit = 0,
          category_ids = [];
        wx.hideLoading();
        for (let value of res.data.data.project.products) {
          area += value.area;
          suit += value.tao;
          category_ids.push(value.category_id);
        }
        that.setData({
          project: res.data.data.project,
          project_name: res.data.data.project.project_name,
          change_project_name: res.data.data.project.project_name,
          area: area,
          suit: suit,
          update_date: res.data.data.project.update_date,
          products: res.data.data.project.products,
          project_id: options.id,
          category_ids: category_ids,
          options: options,
          onshow: true
        })

      }, fail: function () {
        wx.showToast({
          title: '系统错误请稍后重试',
          icon: 'none'
        });
      }
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let options = this.data.options,
        onshow = this.data.onshow;
        if (onshow == true){
          this.onLoad(options)
        }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //添加产品


  insertProduct: function (e) {
    const that = this;
    wx.navigateTo({
      url: '/pages/componet/addProject/addProject?project_id=' + that.data.project_id + '&category_ids=' + that.data.category_ids
    })

  },

  //删除项目
  deleteProject: function () {
    const that = this;
    wx.showModal({
      content: '是否删除此项目',
      success: function (res) {
        if (res.confirm) {
          wx.showToast({
            title: '正在提交信息...',
            icon: 'none',
            duration: 200000
          });
          wx.request({
            url: app.globalData.requestUrl + 'api/project/delete',
            method: 'POST',
            data: {
              user_token: app.globalData.userToken,
              project_id: that.data.project_id,
            },
            success: function (res) {
              wx.hideToast();
              // 执行成功
              wx.navigateBack({
                delta: 1
              });

            }, fail: function () {

              wx.showToast({
                title: '系统错误请稍后重试',
                icon: 'none'
              });
            }
          });

        } else if (res.cancel) {
          wx.showToast({
            title: '删除取消',
            icon: 'none',
          });
        }
      }
    })
  },
  //更新项目名称
  updateProject: function (e) {
    const that = this;
    if (e.detail.value != '') {


      that.setData({
        change_project_name: e.detail.value
      })

    }
  },
  sureChangeProject: function () {
    const that = this;
    let change_project_name = that.data.change_project_name,
      project_name = that.data.project_name;

    if (change_project_name == project_name) {
      wx.showToast({
        title: '项目名未发现更改',
        icon: 'none',
      });
      return;
    }
    wx.showToast({
      title: '正在提交信息...',
      icon: 'none',
      duration: 200000
    });
    wx.request({
      url: app.globalData.requestUrl + 'api/project/edit',
      method: 'POST',
      data: {
        user_token: app.globalData.userToken,
        project_id: that.data.project_id,
        project_name: change_project_name
      },
      success: function (res) {
        wx.hideToast();
        // 执行成功
        wx.navigateBack({
          delta: 1
        });

      }, fail: function () {

        wx.showToast({
          title: '系统错误请稍后重试',
          icon: 'none'
        });
      }
    });
  },
  removeProduct: function (e) {
    const that = this;
    let id = e.currentTarget.id,
      products = that.data.products,
      category_ids = that.data.category_ids,
      index = null;
    wx.showModal({
      content: '是否删除此产品',
      success: function (res) {
        if (res.confirm) {
          wx.showToast({
            title: '正在提交信息...',
            icon: 'none',
            duration: 200000
          });
          wx.request({
            url: app.globalData.requestUrl + 'api/project/delete/product',
            method: 'POST',
            data: {
              user_token: app.globalData.userToken,
              product_id: id,
            },
            success: function (res) {
              wx.hideToast();
              // 执行成功
              if (res.data.status == 1) {

                for (let key of Object.keys(products)) {
                  if (products[key].id == id) {
                    index = key
                  }
                }
                products.splice(index, 1);
                let categorys = [];
                wx.hideLoading();
                for (let value of products) {
                  categorys.push(value.category_id);
                }

                that.setData({
                  products: products,
                  category_ids: categorys
                })
              }

            }, fail: function () {

              wx.showToast({
                title: '系统错误请稍后重试',
                icon: 'none'
              });
            }
          });

        } else if (res.cancel) {
          wx.showToast({
            title: '删除取消',
            icon: 'none',
          });
        }
      }
    })
  }

})